package com.iro.blog.web.admin;

import com.iro.blog.Service.BlogsService;
import com.iro.blog.Service.TagService;
import com.iro.blog.Service.TypeService;
import com.iro.blog.pojo.Blog;
import com.iro.blog.pojo.User;
import com.iro.blog.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin")
public class BlogController {

    private static String INPUT="admin/blogs-input";
    private static String LIST="admin/blogs";
    private static String REDIRECT="redirect:/admin/blogs";

    @Autowired
    private BlogsService blogsService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private TagService tagService;


    @GetMapping("/blogs")
    public String blogs(Model model, BlogQuery blog, @PageableDefault(size = 3,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable){
        model.addAttribute("types",typeService.ListType());
        model.addAttribute("page",blogsService.listBlog(pageable,blog));
        return LIST;
    }

    @PostMapping("/blogs/search")
    public String search(Model model, BlogQuery blog, @PageableDefault(size = 3,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable){
        model.addAttribute("page",blogsService.listBlog(pageable,blog));
        return "admin/blogs :: blogList";
    }

    @GetMapping("/blogs/input")
    public String input(Model model){
       setTypeAndTag(model);
       Blog blog=new Blog();

        model.addAttribute("blog",new Blog());
        return "admin/blogs-input";
    }
    private void setTypeAndTag(Model model){
        model.addAttribute("types",typeService.ListType());
        model.addAttribute("tags",tagService.listTag());
    }

    @GetMapping("/blogs/{id}/input")
    public String editInput(@PathVariable Long id, Model model){
        setTypeAndTag(model);
        Blog blog=blogsService.getBlog(id);
        blog.init();
        model.addAttribute("blog",blog);
        return INPUT;
    }

    @PostMapping("/blogs")
    public String post(Blog blog, RedirectAttributes attributes, HttpSession session){
       blog.setUser((User)session.getAttribute("user"));
       blog.setType(typeService.getType(blog.getType().getId()));
       blog.setTags(tagService.ListTag(blog.getTagIds()));

        Blog b;
        if (blog.getId()==null){
            b=blogsService.saveBlog(blog);
        }else {
            b=blogsService.updateBlog(blog.getId(),blog);
        }
        if (b==null){
            attributes.addFlashAttribute("message","操作失败");

        }else {
            attributes.addFlashAttribute("message","操作成功");
        }
        return "redirect:/admin/blogs";
    }
    @GetMapping("/blogs/{id}/delete")
    public String delete(@PathVariable Long id,RedirectAttributes attributes){
        blogsService.deteleBlog(id);
        attributes.addFlashAttribute("message","删除成功");
        return "redirect:/admin/blogs";
    }
}
